﻿// ////////////////////////////////////////////////////////////////////////////
// 
// SWE Case Tool
// 
// Solution:	CaseTool
// Project:		2. CaseTool GUI
// Filename:	WndChooseMethod.cs
// 
// File Created:	10:05 16.05.2013 
// Last Changed:	18:11 05.06.2013
// 
// ////////////////////////////////////////////////////////////////////////////

using System;
using System.Windows.Forms;

namespace SWE.CaseTool.GUI
{
	/// <summary>
	/// Display's all methods and creates the method and the method gui after selecting one.
	/// </summary>
	public partial class WndChooseMethod : Form
	{
		private readonly MethodFactory _factory;
		private DialogResult _result = DialogResult.Cancel;

		/// <summary>
		/// Initializes a new instance of the <see cref="WndChooseMethod" /> class.
		/// </summary>
		/// <param name="factory">The method factory.</param>
		public WndChooseMethod(MethodFactory factory)
		{
			_factory = factory;

			InitializeComponent();

			try
			{
				var methods = factory.GetMethods();
				if (methods.Count == 0)
				{
					btnChoose.Enabled = false;
					lblDesc.Text =
						@"Es wurden keine Schätz-Methoden gefunden! Speichern Sie Schätzmethoden bitte in den Ordern ""Methods"", um sie zu benutzen.";
				}
				else
				{
					lbMethods.Items.AddRange(methods.ToArray());
					lbMethods.SelectedIndex = 0;
					LbMethodsSelectedIndexChanged(this, EventArgs.Empty);
				}
			}
			catch (Exception ex)
			{
				btnChoose.Enabled = false;
				MessageBox.Show(this, ex.Message, @"Unerwarteter Fehler");
			}
		}

		/// <summary>
		/// Shows the form as a modal dialog box with the specified owner.
		/// </summary>
		/// <param name="owner">
		/// Any object that implements <see cref="T:System.Windows.Forms.IWin32Window" /> that represents the
		/// top-level window that will own the modal dialog box.
		/// </param>
		/// <returns>
		/// One of the <see cref="T:System.Windows.Forms.DialogResult" /> values.
		/// </returns>
		/// <PermissionSet>
		///     <IPermission
		///         class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
		///         version="1" Unrestricted="true" />
		///     <IPermission
		///         class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
		///         version="1" Unrestricted="true" />
		///     <IPermission
		///         class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
		///         version="1" Flags="UnmanagedCode, ControlEvidence" />
		///     <IPermission
		///         class="System.Security.Permissions.UIPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
		///         version="1" Unrestricted="true" />
		///     <IPermission
		///         class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
		///         version="1" Unrestricted="true" />
		/// </PermissionSet>
		public new DialogResult ShowDialog(IWin32Window owner = null)
		{
			base.ShowDialog(owner);
			return _result;
		}

		/// <summary>
		/// OnClick handler for button choose. If the method and it's gui could could be instantiated correctly,
		/// the dialog will close with DialogResult.Ok, otherwise a error message is displayed.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void BtnChooseClick(object sender, EventArgs e)
		{
			if (lbMethods.SelectedIndex < 0)
			{
				MessageBox.Show(this, @"Bitte wählen Sie eine Methode aus, bevor sie vor fahren", @"Keine Methode gewählt");
			}
			else
			{
				try
				{
					var selected = (lbMethods.SelectedItem ?? "").ToString();
					Method = _factory.CreateMethod(selected);
					Window = _factory.CreateWindow(selected, Method);
					_result = DialogResult.OK;
					Close();
				}
				catch (Exception ex)
				{
#if DEBUG
					MessageBox.Show(this, ex.Message, @"Unerwarteter Fehler");
#else
	// TODO: LOG
#endif
				}
			}
		}

		/// <summary>
		/// OnClick handler for button cancel. The dialog will close with DialogResult.False
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void BtnCancelClick(object sender, EventArgs e)
		{
			_result = DialogResult.OK;
			Close();
		}

		/// <summary>
		/// LbMethods index changed handler. Updates the description and the name methodType after the selction
		/// changed.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void LbMethodsSelectedIndexChanged(object sender, EventArgs e)
		{
			var selected = (lbMethods.SelectedItem ?? "").ToString();
			if (!string.IsNullOrWhiteSpace(selected))
			{
				try
				{
					var description = _factory.GetMethodDescription(selected);
					lblDesc.Text = String.IsNullOrWhiteSpace(description) ? "Keine Beschreibung vorhanden" : description;
				}
				catch (Exception ex)
				{
#if DEBUG
					MessageBox.Show(this, ex.Message, @"Unerwarteter Fehler");
#else
	// TODO: LOG
#endif
				}
			}
			else
			{
				lblDesc.Text = @"Keine Beschreibung vorhanden";
			}
		}

		/// <summary>
		/// Gets the method intance.
		/// </summary>
		/// <value>
		/// The method instance.
		/// </value>
		public IMethod Method { get; private set; }

		/// <summary>
		/// Gets the method window.
		/// </summary>
		/// <value>
		/// The method window.
		/// </value>
		public Form Window { get; private set; }
	}
}